<template>
  <el-dialog
    :before-close="handleClose"
    title="状态变更"
    :visible.sync="dialogVisible"
  >
    <el-form
      ref="ruleForm"
      v-loading="loading"
      label-width="100px"
      :model="ruleForm"
      :rules="rules"
      size="mini"
    >
      <el-form-item label="状态" prop="status_id">
        <el-select v-model="ruleForm.status_id">
          <el-option
            v-for="(item, index) in status_list"
            :key="index"
            :label="item.value"
            :value="item.key"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submit('ruleForm')">保存</el-button>
        <el-button @click="dialogVisible = false">返回</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import threat from "@/api/threat";
import clearWatch from "@/utils/mixin/clearWatch";
export default {
  name: "StateChanges",
  mixins: [clearWatch],
  data() {
    return {
      dialogVisible: false,
      loading: false,
      ruleForm: {
        status_id: "",
        str: "",
        array: [],
        obj: {},
        num: 1
      },
      rules: {
        status_id: [
          { required: true, message: "请选择状态", trigger: "change" }
        ]
      },
      org_id: "",
      threat_id: "",
      status_list: []
    };
  },

  methods: {
    handleClose(done) {
      done();
    },
    show(org_id, threat_id) {
      this.dialogVisible = !this.dialogVisible;
      this.loading = true;
      this.org_id = org_id;
      this.threat_id = threat_id;
      this.loadingStatus();
    },
    loadingStatus() {
      threat.status_list().then(res => {
        let { status_list } = res.data;
        status_list = Object.keys(status_list).map(k => {
          return {
            key: k,
            value: status_list[k]
          };
        });
        this.status_list = status_list;
        this.loading = false;
      });
    },

    submit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          threat
            .status({
              org_id: this.org_id,
              threat_id: this.threat_id,
              status_id: this.ruleForm.status_id
            })
            .then(res => {
              this.dialogVisible = false;
              this.$message.success(res.data.message);
              this.$emit("change");
            });
        } else {
          return false;
        }
      });
    }
  }
};
</script>
